package com.yida.gwj.config.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfDictData;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.common.domain.dto.GwjUserDTO;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.config.domain.dto.GwjBusinessFlowDTO;
import com.yida.gwj.config.domain.request.GwjConfigFlowBusinessRequest;
import com.yida.gwj.config.mapper.GwjBusinessCommonMapper;
import com.yida.gwj.config.service.IGwjBusinessCommonService;
import com.yida.gwj.config.service.IGwjConfDictDataService;
import com.yida.gwj.config.service.IGwjConfStepService;

import cn.hutool.core.util.ObjectUtil;

/**
 * @Author: PPV
 * @Date: 2023-02-24
 */
@Service
public class GwjBusinessCommonServiceImpl implements IGwjBusinessCommonService {

    @Resource
    private IGwjConfStepService stepService;

    @Resource
    private IGwjConfDictDataService dictDataService;

    @Resource
    private GwjBusinessCommonMapper commonMapper;

    @Override
    public List<GwjBusinessFlowDTO> getByBizId(GwjConfigFlowBusinessRequest request) {
        List<GwjConfStep> steps = stepService.selectGwjConfStepListByFlagId(request.getBizFlagId());
        List<GwjBusinessFlowDTO> flows = GwjConvertUtil.convertList(steps, GwjBusinessFlowDTO.class);

        // 获取表名中缀
        GwjConfDictData dictData = dictDataService.getByDictLabel(request.getDictLabel());
        String midFix = dictData.getDictValue();
        // 业务表名拼接
        String bizTable = new StringBuilder().append("gwj_").append(midFix).append("_business").toString();
        // 日志表名拼接
        String logTable = new StringBuilder().append("gwj_").append(midFix).append("_business_log").toString();

        if (ObjectUtil.isNull(request.getBizId())) {
            flows.get(0).setCurrentOperator(SecurityUtils.getLoginUser().getSysUser().getRealName());
            flows.get(0).setCurrent(true);
        } else {
            // 组装过滤参数
            HashMap<String, Object> paramMap = new HashMap<String, Object>(4) {
                {
                    put("bizId", request.getBizId());
                    // put("result", GwjBusinessLogResult.SUBMIT.getCode());
                    put("logTable", logTable);
                }
            };
            if (GwjBusinessFlagType.isOutSide(request.getBizFlagId())) {
                // 外网业务 走外网数据库
                GwjBusinessFlowDTO businessCurrentFlow = commonMapper.getOutFlow(request.getBizId(), bizTable);
                try {
                    flows.stream().forEach(item -> {
                        if (item.getStepId().equals(businessCurrentFlow.getStepId())) {
                            item.setCurrent(true);
                            item.setCurrentOperator(businessCurrentFlow.getCurrentOperator());
                            throw new SecurityException("");
                        } else {
                            paramMap.put("stepId", item.getStepId());
                            GwjUserDTO previousSubmitUser = commonMapper.getOutPreviousSubmitUser(paramMap);
                            if (ObjectUtil.isNotEmpty(previousSubmitUser)) {
                                item.setCurrentOperator(previousSubmitUser.getRealName());
                            }
                        }
                    });
                } catch (SecurityException e) {
                }
            } else {
                GwjBusinessFlowDTO businessCurrentFlow = commonMapper.getFlow(request.getBizId(), bizTable);
                try {
                    flows.stream().forEach(item -> {
                        if (item.getStepId().equals(businessCurrentFlow.getStepId())) {
                            item.setCurrent(true);
                            item.setCurrentOperator(businessCurrentFlow.getCurrentOperator());
                            throw new SecurityException("");
                        } else {
                            paramMap.put("stepId", item.getStepId());
                            GwjUserDTO previousSubmitUser = commonMapper.getPreviousSubmitUser(paramMap);
                            if (ObjectUtil.isNotEmpty(previousSubmitUser)) {
                                item.setCurrentOperator(previousSubmitUser.getRealName());
                            }
                        }
                    });
                } catch (Exception e) {
                }
            }
        }
        return flows;
    }

    @Override
    public TableDataInfo getLogTable(Long bizId, String dictLabel, Integer outSideFlag) {
        // 获取表名中缀
        GwjConfDictData dictData = dictDataService.getByDictLabel(dictLabel);
        String midFix = dictData.getDictValue();
        // 日志表名拼接
        String logTable = new StringBuilder().append("gwj_").append(midFix).append("_business_log").toString();
        // 日志分页
        PageUtils.startPage();
        if (1 == outSideFlag) {
            // 外网日志 切换外网数据库
            return PageUtils.getDataTable(commonMapper.getOutLog(bizId, logTable));
        }
        return PageUtils.getDataTable(commonMapper.getLog(bizId, logTable));
    }

}
